package com.chenyi.web.controller.shop;

import com.chenyi.common.annotation.Log;
import com.chenyi.common.annotation.UserRequired;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.entity.SysAdmin;
import com.chenyi.common.core.domain.entity.SysAdminRole;
import com.chenyi.common.core.domain.entity.SysShop;
import com.chenyi.common.core.page.TableDataInfo;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.enums.OperatorType;
import com.chenyi.common.utils.AdminUtils;
import com.chenyi.common.utils.ShopUtils;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.shop.domain.SysShopRole;
import com.chenyi.shop.service.ISysShopRoleService;
import com.chenyi.shop.service.ISysShopService;
import com.chenyi.system.service.ISysAdminRoleService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商户管理Controller
 *
 * @author chenyi
 * @date 2020-10-22
 */
@RestController
@RequestMapping("/shop/shop")
public class ShopController extends BaseController {
    @Autowired
    private ISysShopService sysShopService;

    @Autowired
    private ISysShopRoleService roleService;
    /**
     * 查询商户管理列表
     */
    @UserRequired
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysShop sysShop)
    {
        startPage();
        sysShop.setCreateBy(ShopUtils.getUserName());
        List<SysShop> list = sysShopService.selectSysShopList(sysShop);
        return new ResponseEntity<>(getDataTable(list), HttpStatus.OK);
    }

    /**
     * 导出商户管理列表
     */
    @UserRequired
    @Log(title = "商户管理", businessType = BusinessType.EXPORT,operatorType = OperatorType.SHOP)
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysShop sysShop)
    {
        List<SysShop> list = sysShopService.selectSysShopList(sysShop);
        ExcelUtil<SysShop> util = new ExcelUtil<SysShop>(SysShop.class);
        return util.exportExcel(list, "shop");
    }

    /**
     * 获取商户管理详细信息
     */
    @UserRequired
    @GetMapping(value = { "/" , "/{shopId}" })
    public ResponseEntity<Object> getInfo(@PathVariable(value = "shopId", required = false)  Long shopId)
    {
        Map<String,Object> data = Maps.newHashMap();
        List<SysShopRole> roles = roleService.selectShopRoleAll();
        data.put("roles", roles);
        if (StringUtils.isNotNull(shopId))
        {
            data.put("data", sysShopService.selectSysShopById(shopId));
        }
        return ResponseEntity.ok(data);
    }

    /**
     * 新增商户管理
     */
    @UserRequired
    @Log(title = "商户管理", businessType = BusinessType.INSERT,operatorType = OperatorType.SHOP)
    @PostMapping
    public ResponseEntity<Object> add(@RequestBody SysShop sysShop)
    {
        sysShop.setPassword(ShopUtils.encryptPassword(sysShop.getPassword()));
        sysShop.setCreateBy(ShopUtils.getUserName());
        sysShopService.insertSysShop(sysShop);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改商户管理
     */
    @UserRequired
    @Log(title = "商户管理", businessType = BusinessType.UPDATE,operatorType = OperatorType.SHOP)
    @PutMapping
    public ResponseEntity<Object> edit(@RequestBody SysShop sysShop)
    {
        sysShop.setUpdateBy(ShopUtils.getUserName());
        sysShopService.updateSysShop(sysShop);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除商户管理
     */
    @UserRequired
    @Log(title = "商户管理", businessType = BusinessType.DELETE,operatorType = OperatorType.SHOP)
    @DeleteMapping("/{shopIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] shopIds)
    {
        sysShopService.deleteSysShopByIds(shopIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
